import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/ball_pulse_footer.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_shop/components/empty.dart';
import 'package:flutter_shop/models/categroyModel.dart';
import 'package:flutter_shop/provider/category.dart';
import 'package:provider/provider.dart';

class CategoryGoodList extends StatefulWidget {
  @override
  _CategoryGoodListState createState() {
    return _CategoryGoodListState();
  }
}

class _CategoryGoodListState extends State<CategoryGoodList> {
  int page = 1;
  bool noDataFlag = false;

  Future _onRefreshLoad(CategoryProvider categoryProvider) async {
    this.page++;
    var data = await categoryProvider.getGoodList(this.page);
    if (List.from(data).length == 0) {
      this.noDataFlag = true;
    }
    categoryProvider.appendGoodList(data);
  }

  Widget _listViewItem(List<CategoryGoodsModel> list, int index) {
    bool lastSingle = index + 1 >= list.length;
    return Padding(
      padding: EdgeInsets.fromLTRB(5.0, 0, 5.0, 0),
      child: Row(
        mainAxisAlignment: lastSingle
            ? MainAxisAlignment.start
            : MainAxisAlignment.spaceBetween,
        children: <Widget>[
          this._goodItem(list[index]),
          lastSingle ? Text('') : this._goodItem(list[index + 1])
        ],
      ),
    );
  }

  Widget _goodItem(CategoryGoodsModel good) {
    return InkWell(
      onTap: () {
        print("click me");
      },
      child: Container(
        width: ScreenUtil().setWidth(260),
        color: Colors.white,
        padding: EdgeInsets.all(5.0),
        margin: EdgeInsets.only(bottom: 3.0),
        child: Column(
          children: <Widget>[
            Image.network(
              good.image,
              width: ScreenUtil().setWidth(200),
            ),
            Text(
              good.goodsName,
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                  color: Colors.pink, fontSize: ScreenUtil().setSp(26)),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Text("￥${good.presentPrice}",
                    style: TextStyle(fontSize: ScreenUtil().setSp(24))),
                Text("￥${good.oriPrice}",
                    style: TextStyle(
                        color: Colors.black26,
                        fontSize: ScreenUtil().setSp(24),
                        decoration: TextDecoration.lineThrough)),
              ],
            )
          ],
        ),
      ),
    );
  }

  List<Widget> _renderListView(List<CategoryGoodsModel> list) {
    List<Widget> listWidget = [];
    for (var i = 0; i < list.length; i += 2) {
      listWidget.add(this._listViewItem(list, i));
    }
    if (this.noDataFlag) {
      listWidget.add(Center(
        child: Text("没有更多数据了..."),
      ));
    }
    return listWidget;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: ScreenUtil().setWidth(570.0),
      height: ScreenUtil().setHeight(1000.0),
      child: Consumer<CategoryProvider>(
        builder: (context, category, _) {
          if (category.categoryGoodsList.length == 0) {
            return EmptyWidget();
          }
          return EasyRefresh(
              onLoad: this.noDataFlag
                  ? null
                  : () async {
                      await this._onRefreshLoad(category);
                    },
              footer: BallPulseFooter(),
              child: ListView(
                children: this._renderListView(category.categoryGoodsList),
              ));
        },
      ),
    );
  }
}
